<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBillsTableWhenTesting extends Migration
{
    protected $connection_billing = 'billing';
    protected $connection_cash_billing = 'cash_billing';
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (app()->environment() === 'testing') {
            Schema::connection($this->connection_billing)->create('bills', function (Blueprint $table) {
                $table->timestamp('updated_at')->useCurrent();
                $table->timestamp('created_at');
                //
                $table->increments('id');
                $table->string('title', 100);

                $table->integer('product_id');
                $table->integer('student_id');

                $table->decimal('price');
                $table->decimal('discount')->default(0);
                $table->decimal('actual_price');

                $table->tinyInteger('status')->default(10);

                $table->string('pay_channel', 32);
                $table->string('pay_sn', 64);
                $table->string('pay_info', 2000);
                $table->timestamp('paid_at')->nullable();

                $table->softDeletes();
            });
            Schema::connection($this->connection_cash_billing)->create('bills', function (Blueprint $table) {
                $table->timestamp('updated_at')->useCurrent();
                $table->timestamp('created_at');

                $table->increments('id');
                $table->string('title', 100);

                $table->integer('student_id');
                $table->integer('refund_id')->nullable();

                $table->integer('recharge_level_id')->default(0);
                $table->decimal('recharge_amount')->default(0);
                $table->decimal('cash_return')->default(0);
                $table->decimal('actual_price')->default(0);
                $table->decimal('refund_amount')->default(0);

                $table->tinyInteger('status')->default(10);

                $table->string('prepay_id', 255);
                $table->string('pay_channel', 32);
                $table->tinyInteger('pay_type');
                $table->string('pay_sn', 64);
                $table->string('pay_info', 2000);
                $table->timestamp('paid_at')->nullable();

                $table->softDeletes();
                $table->index('student_id');
                $table->index('status');
                $table->index('recharge_level_id');
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        if (app()->environment() === 'testing') {
            Schema::connection($this->connection_billing)->drop('bills');
            Schema::connection($this->connection_cash_billing)->drop('bills');
        }
    }
}
